import FetchUtils from '../Fetch';

/**
 * 生成nft
 * @param {"userToken","name","img","link","descrption"} param
 */
export function generalNFT(param, images) {
  return FetchUtils.post('/auction/nft/new', param, images, 60000);
}

/**
 * 生成auction
 * @param {"userToken","minBid","expirationDate","itemId"} param
 */
export function createAuction(param) {
  return FetchUtils.post('/auction/new', param);
}

/**
 * 竞拍
 * @param {"userToken","hash"} param
 */
export function offerPrice(param) {
  return FetchUtils.post('/auction/offer/preview', param);
}

/**
 * 接受价格
 * @param {"userToken", "offerId"} param
 */
export function acceptPrice(param) {
  return FetchUtils.post('/auction/offer/accept', param);
}

/**
 * 获取拍卖列表
 * @param {page,size} param
 * status 0-正在拍卖，1-已竞拍结束，2-流拍，3-待发货，4-已发货，9-已取消
 */
export function queryAuctionList(param) {
  return FetchUtils.get('/auction/list', param);
}

/**
 * 获取nft列表
 * @param {userToken,page,size} param
 */
export function queryItemsList(param) {
  return FetchUtils.get('/auction/items/list', param);
}

/**
 * 获取拍卖出价
 * @param {"userToken", "actionId",page,size} param
 */
export function queryAuctionOffer(param) {
  return FetchUtils.get('/auction/offers/list', param);
}

/**
 * 获取拍卖出价
 * @param {"userToken", "type"，page,size} param
 */
export function queryUserOffers(param) {
  return FetchUtils.get('/auction/offers/user', param);
}

/**
 * 获取拍卖详情
 * @param {"userToken", "auctionId"} param
 */
export function queryAuctionDetail(param) {
  return FetchUtils.get('/auction/detail', param);
}

/**
 * 取消拍卖
 * @param {"userToken", "auctionId"} param
 */
export function cancelListingAuction(param) {
  return FetchUtils.post('/auction/cancel', param);
}

/**
 * 取消出价
 * @param {"userToken", "offerId"} param
 */
export function cancelAuctionOffer(param) {
  return FetchUtils.post('/auction/offer/cancel', param);
}

/**
 * 记录拍卖事件
 * @param {"userToken", "auctionId"} param
 */
export function recoderAuctionEvent(param) {
  return FetchUtils.post('/auction/event/new', param);
}

/**
 * 查询拍卖事件
 * @param {"auctionId"} param
 */
export function queryAuctionEvents(param) {
  return FetchUtils.get('/auction/event/list', param);
}

/**
 * 获取token
 * @param {"userToken"} param
 */
export function queryUploadToken(param) {
  return FetchUtils.get('/upload/token', param);
}

/**
 * 导入nft
 * @param {"userToken", "tokenId","contract"} param
 */
export function importNft(param) {
  return FetchUtils.post('/auction/nft/import', param);
}

/**
 * 上传文件至七牛
 * @param {file,key,token,putExtra} param
 */
export async function uploadImageToQiNiu(param) {
  let formData = new FormData();
  Object.keys(param)
    .filter((key) => key != 'file')
    .forEach((key) => {
      formData.append(key, param[key]);
    });

  if (param.file.uri) {
    const imagefile = {
      uri: param.file.uri,
      type: param.putExtra.mimeType || 'image/jpeg',
      name: param.key,
      filename: param.key,
    };
    console.log(imagefile);
    formData.append('file', imagefile);
  } else {
    formData.append('file', param.file);
  }

  console.log('上传配置');
  console.log(JSON.stringify(formData));

  // const headers = new Headers();
  // headers.append('Content-Type', 'multipart/form-data');
  // headers.append('Accept', 'application/json');

  return new Promise((resolve, reject) => {
    fetch('https://up-as0.qiniup.com/', {
      method: 'POST',
      body: formData,
      headers: {},
    })
      .then((response) => {
        console.log(response);
        console.log(response.text());
        console.log(response.json());
        return response.json();
      })
      .then((responseJson) => {
        console.log('响应结果');
        console.log(JSON.stringify(responseJson));
        resolve(responseJson);
      })
      .catch((err) => {
        console.log('上传出错');
        console.log(err);
        reject(err);
      });
  });
}

/**
 * 注册nft资源
 * @param {image,file,name,link,description} param
 * @returns
 */
export async function registerNftSource(param, images) {
  return FetchUtils.post('/auction/nft/register', param, images);
}
